/**
 * Created by IntelliJ IDEA.
 * @Author: xuybang
 * @version: 2017/5/25.
 * To change this template use File | Settings | File Templates.
 */
(function () {

    var $view;
    var $template;
    var $contentUl;

    function onInit() {
        onInitData();
        app.onInitSwiper(".swiper-container");
        onGetReleaseContent();
    }

    function onInitData() {

        $template = $view.find("li.template");
        $template.removeClass("template").remove();

        $contentUl = $view.find('.content_ul');

        //延迟加载图片
        $("img.lazy_load").lazyload({
            // event: "scrollstop", //滚动加载
            effect: "fadeIn" //淡入
        });
    }

    // 获取文章内容
    function onGetReleaseContent() {
        backService.find.onGetReleaseContent(function (jsons) {
            var datas = jsons.data;
            if (datas.length > 0) {
                for (var i = 0; i < datas.length; i++) {
                    var data = datas[i];
                    var $item = $template.clone();
                    var con = data.content;
                    if (con != null && con.indexOf("<img") != -1) {
                        var div = onChangeStrToDom(con);
                        var $img = $(div).find("img").eq(0);
                        // console.log(base64);
                        $item.find(".image_con").attr("src", $img.attr("src"));
                        $item.find(".image_con").fadeIn();

                    }
                    if (con != null && con.indexOf("<p") != -1) {
                        var div = onChangeStrToDom();
                        var $p = $(div).find("p").eq(0);
                        $item.find('.desc').html($p);
                        if ($item.find('.desc').find("img").length > 0) {
                            $item.find('.desc p img').remove();
                        }
                    } else {
                        $item.find('.desc').html("");
                    }
                    $item.find('.body .avatar').text(data.nickname);
                    $item.find('.date').text(pub.date.formatDateTime(data.createTime));
                    $item.find('.title').text(data.title);

                    $item.appendTo($contentUl);
                }
            }
        }, function (e) {
            app.showToast(e.msg);
        });

    }

    function onChangeStrToDom(con) {
        var div = document.createElement("div");
        if (typeof con == "string")
            div.innerHTML = con;
        return div;
    }

    /**
     * 滚动监听事件
     * @type {{pageScroll: pageScroll}}
     */
    var scroll = {
        pageScroll: function () {
            var scrollTop = document.documentElement.scrollTop === 0 ? $("#find_index").find(".km-content").get(0).scrollTop : document.documentElement.scrollTop;
            if (scrollTop > 20) {// 到底了
                $view.find('.search_model').addClass('show');
            } else {
                $view.find('.search_model').removeClass('show');
            }
        }
    };
    /*初始化*/
    window.onViewInit_find_index = function (view) {
        $view = view.element;

        // 滚动修改标题栏颜色
        $view.find(".km-content").scroll(function () {
            scroll.pageScroll();
        });
        onInit();
    };

    window.onViewShow_find_index = function (view) {
        $view = view.element;

        app.onBackTabItemActive($view, 1);
    };
})();